﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Web.Song.ViewModels;
using System.Globalization;
using System.Net;
using System.Web.Security;
using HelloMinasan.Business;
using HelloMinasan.Framework.Web;
using HelloMinasan.Business.Extend;

namespace HelloMinasan.Web.Song.Controllers
{

    public class AccountController : ApplicationController
    {
        //private UserUnitWork _userunitWork = new UserUnitWork();
        //private readonly Encryptor _encryptor = new Encryptor();

        private ISongService songService;
        private IUserService userService;

        public AccountController(ISongService songService, IUserService userService)
        {
            this.songService = songService;
            this.userService = userService;
        }

        [HttpGet]
        //[ValidateAntiForgeryToken]
        public ActionResult LogOnPartial()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult LogOnPartial(LogOnModel model)
        {

            return PartialView(model);
        }



        ////[AllowAnonymous]
        //public ActionResult LogOn(string returnUrl)
        //{           
        //    if (CurrentUser != null)
        //    {
        //        return RedirectToAction("Index", "Home");
        //    }
        //    ViewBag.ReturnUrl = returnUrl;
        //    //get cookie
        //    var cookie = Request.Cookies["RememberMe"];
        //    if (cookie != null)
        //    {
        //        LogOnModel model = new LogOnModel();
        //        model.UserName = cookie["UserName"].ToString();
        //        model.Password = cookie["Password"].ToString();
        //        User user = _userunitWork.User.All.ToList().Where(u => u.Username == model.UserName && u.Pass == model.Password).FirstOrDefault();

        //        if (user.IsLocked == true)
        //        {
        //            ModelState.AddModelError(string.Empty, Resource.Song_User_locked);
        //            return View(model);
        //        }
        //        else if (user.Active == UserActive.NonActive.ToString())
        //        {
        //            ModelState.AddModelError(string.Empty, Resource.Song_Email_inactive);
        //            return View(model);
        //        }

        //        if (user != null)
        //        {
        //            WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe);
        //            SaveSessionUser(user);
        //            MvcApplication.htUserOnline.Add(Session.SessionID, model.UserName);
        //            // set cokie
        //            if (model.RememberMe)
        //            {
        //                cookie = new HttpCookie("RememberMe");
        //                cookie["UserName"] = model.UserName;
        //                cookie["Password"] = model.Password;
        //                Response.Cookies.Add(cookie);
        //            }
        //            else
        //            {
        //                cookie = new HttpCookie("RememberMe");
        //                cookie["UserName"] = string.Empty;
        //                cookie["Password"] = string.Empty;
        //                cookie.Expires = DateTime.Now.AddDays(-1);
        //                Response.Cookies.Add(cookie);
        //            }
        //            RedirectToLocal(returnUrl);
        //        }
        //        else
        //        {
        //            ModelState.AddModelError(string.Empty, Resource.Song_EmptyTest);
        //            return View(model);
        //        }
        //    }
        //    return View();
        //}

        //[HttpPost]
        ////[AllowAnonymous]
        //public ActionResult LogOn(LogOnModel model, string returnUrl)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {                       
        //            string username = model.UserName.ToLower().Trim();
        //            string password = _encryptor.Encode(model.Password);
        //            User user = _userunitWork.User.All.ToList().Where(u => u.Username.ToLower().Trim() == username && u.Pass == password).FirstOrDefault();
        //            if (user == null)
        //            {
        //                ModelState.AddModelError(string.Empty, "User or Password is not correct");
        //                return View(model);
        //            }
        //            if (user.IsLocked==true)
        //            {
        //                ModelState.AddModelError(string.Empty, Resource.Song_User_locked);
        //                return View(model);
        //            }
        //            else if(user.Active == UserActive.NonActive)
        //            {
        //                ModelState.AddModelError(string.Empty, Resource.Song_Email_inactive);
        //                return View(model);
        //            }
               

        //            if (user != null)
        //            {
                        
        //                    WebSecurity.Login(model.UserName, model.Password, true);
        //                    SaveSessionUser(user);

        //                    if (!MvcApplication.htUserOnline.ContainsKey(Session.SessionID))
        //                    {
        //                        MvcApplication.htUserOnline.Add(Session.SessionID, model.UserName);
        //                    }

        //                    //set cokie
        //                    if (model.RememberMe)
        //                    {
        //                        HttpCookie cookie = new HttpCookie("RememberMe");
        //                        cookie["UserName"] = model.UserName;
        //                        cookie["Password"] = model.Password;
        //                        Response.Cookies.Add(cookie);
        //                    }
        //                    else
        //                    {
        //                        HttpCookie cookie = new HttpCookie("RememberMe");
        //                        cookie["UserName"] = string.Empty;
        //                        cookie["Password"] = string.Empty;
        //                        cookie.Expires = DateTime.Now.AddDays(-1);
        //                        Response.Cookies.Add(cookie);
        //                    }

        //                }
                     
        //            else
        //            {
        //                ModelState.AddModelError(string.Empty, "UserName or PassWord is wrong!");
        //                return View(model);

        //            }
        //            return RedirectToLocal(returnUrl);
        //        }
        //        catch (Exception)
        //        {

        //            RedirectToLocal(returnUrl);
        //        }

        //        return RedirectToLocal(returnUrl);
        //    }

        //    return View(model);
        //}
        
        //public ActionResult LogOff()
        //{
        //    HttpCookie cookie = new HttpCookie("RememberMe");
        //    cookie["UserName"] = string.Empty;
        //    cookie["Password"] = string.Empty;
        //    cookie.Expires = DateTime.Now.AddDays(-1);
        //    Response.Cookies.Add(cookie);
        //    MvcApplication.htUserOnline.Remove(Session.SessionID);
        //    WebSecurity.Logout();
        //    if (this.SessionManager.CurrentUser != null)
        //        //Xoa session
        //        RemoveSessionUser();
        //    return RedirectPermanent("Logon");
        //}
        
        //[AllowAnonymous]
        public ActionResult Register()
        {   RegisterModel model= new RegisterModel();
            model.Day= DateTime.Now.Day;
            model.Month = DateTime.Now.Month;
            model.Year= DateTime.Now.Year;

            ViewBag.Days = new SelectList(Enumerable.Range(1, 31).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Day);
            ViewBag.Months = new SelectList(Enumerable.Range(1, 12).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Month);
            ViewBag.Years = new SelectList(Enumerable.Range(DateTime.Now.Year - 113, 114).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Year);
            return View("Register", model);
        }

        //[HttpPost]
        ////[AllowAnonymous]
        //public ActionResult Register(RegisterModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        #region validate
        //        //if name exits

        //        ViewBag.Days = new SelectList(Enumerable.Range(1, 31).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Day);
        //        ViewBag.Months = new SelectList(Enumerable.Range(1, 12).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Month);
        //        ViewBag.Years = new SelectList(Enumerable.Range(DateTime.Now.Year - 120, 120).Select(i => new { Id = i, Text = i }), "Id", "Text", model.Year);

        //        var list1 = _userunitWork.User.All.Where(n => n.Username == model.UserName).ToList();
        //        if (list1.Count() > 0)
        //        { 
        //            ModelState.AddModelError("UserName", Resource.Song_User_exist);
        //            return View(model);
        //        }
        //        //if email exits
        //        var list = _userunitWork.User.All.Where(n => n.Email == model.Email.ToLower()).ToList();
        //        if(list.Count()>0)
        //        {
        //            ModelState.AddModelError("Email", Resource.Song_Email_Exit);
        //            return View(model);
        //        }

        //        int dayonmonth = DateTime.DaysInMonth(model.Year, model.Month);
        //        if (model.Day > dayonmonth)
        //        {   
        //            ModelState.AddModelError("Day", "The selected date is not valid.");
        //            return View(model);
        //        }
          
        //        #endregion

        //        // new register if not exits
        //        User User = new User();
        //        User.Fullname = model.FullName;
        //        User.Username = model.UserName;                
        //        User.Email = model.Email.ToLower();
        //        User.Pass = CommonData.MD5(model.ConfirmPassword.Trim());
        //        User.Active = UserActive.NonActive;
        //        User.DateOfBirth = DateTime.ParseExact(CommonData.DateParse(model.Day, model.Month, model.Year).ToString("MM/dd/yyyy",CultureInfo.InvariantCulture) , "MM/dd/yyyy", new CultureInfo("en-US")); 
        //        _userunitWork.User.Insert(User);

        //        string FromEmail = Settings.Default.EmailAdmin;
        //        string subject = "User Register";
        //        string link = "http://localhost:8748/Account/Active?User="+model.UserName+"";
        //        string EmailTo = model.Email;
        //        string Body = "Hi '" + model.UserName + "', <br/><br/>";
        //        Body += "You or someone used this email to registed account on Learnenglishbysong.com.<br/>";
        //        Body += "hãy vào Link bên dưới để hoàn tất quá trình đăng ký: <a href="+link+">http://localhost:8748/Account/Active</a>";
        //        //Sent Mail
        //        CommonData.SendMail(FromEmail,Settings.Default.DisplayName,model.Email,subject, Body);
        //        return Redirect(Url.Action("Index","Home"));

        //    }

        //    return View(model);
        //}

        //public ActionResult Active()
        //{
        //    string UActive = Request.QueryString["User"].Trim();
        //    var query = _userunitWork.User.Where(n => n.Username == UActive).ToList();
        //    if(query.Count()>0)
        //    {
        //        try
        //        {
        //            User User = new User();
        //            User = query.First();
        //            User.Active = UserActive.Active;
        //            _userunitWork.User.Update(User);
                    
        //            WebSecurity.Login(User.Username, User.Pass,false);
        //            SaveSessionUser(User);

        //            if (WebSecurity.UserExists(User.Username))
        //            {
        //                FormsAuthentication.SetAuthCookie(User.Username, false);
        //            }
                    
        //            if (!MvcApplication.htUserOnline.ContainsKey(Session.SessionID))
        //            {
        //                MvcApplication.htUserOnline.Add(Session.SessionID, User.Username);
        //            }

        //            return Redirect(@Url.Action("UpdateProfile"));
        //        }
        //        catch
        //        {
        //        }
                
        //    }
        //    return Redirect(@Url.Action("Index", "Home"));           
            
        //}

        ////[AllowAnonymous]
        //public ActionResult ChangePassword()
        //{
        //    return View("ChangePassword", new ChangePasswordModel());
        //}

        ////[AllowAnonymous]
        //[HttpPost]
        //public ActionResult ChangePassword(ChangePasswordModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var Newpass = CommonData.MD5(model.OldPassword);
        //        var CurrentUser = GetCurrentUser().Username;
        //        var List = _userunitWork.User.Where(n => n.Username == CurrentUser && n.Pass == Newpass).ToList();

        //        if (List.Count() > 0)
        //        {
        //            User user = List.First();
        //            user.Pass = CommonData.MD5(model.ConfirmPassword);
        //            _userunitWork.User.Update(user);
        //            ViewBag.success = "Password has been changed";
        //            return View();
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("OldPassword", "Password is not correct");
        //            return View(model);
        //        }
        //    }
        //    return View(model);
        //}

        //[HttpGet]
        //public ActionResult ResetPassWord()
        //{
        //    HelloMinasan.Web.Song.ViewModels.ResetPassWord model = new ResetPassWord();
        //    model.EmailType = true;
        //    return View(model);
        //}

        //[HttpPost]
        //public ActionResult ResetPassWord(ResetPassWord model)
        //{
        //    string EmailReset = model.Email;
        //    string UserName = "";
        //    if (model.EmailType == false)
        //    {
        //        model.Email = "Phamdocaohiep@gmail.com";
        //    }
        //    else
        //    {
        //        model.PhoneNumber = "0977896565";
        //    }
        //    if (ModelState.IsValid)
        //    {
        //        User user = new User();
        //        if (model.EmailType == true)
        //        {
        //            try
        //            {
        //                user = _userunitWork.User.Where(n => n.Email == EmailReset).First();
        //            }
        //            catch
        //            {

        //            }
        //            if (string.IsNullOrEmpty(user.Email))
        //            {
        //                ModelState.AddModelError(string.Empty, "Email does not exist!");
        //                return View(model);
        //            }
        //            if (user.IsLocked == UserLocked.Locked)
        //            {
        //                ModelState.AddModelError(string.Empty, "This account already locked, please contact system administrator to unlocked ");
        //                return View(model);
        //            }

        //            if (user != null && user.Email != "")
        //            {
        //                string Rcodew = CommonData.CreatePassword(6);
        //                UserName = user.Username;
        //                user.Rcode = Rcodew;
        //                _userunitWork.User.Update(user);

        //                string FromEmail = Settings.Default.EmailAdmin;
        //                string FromName = Settings.Default.DisplayName;
        //                string subject = "Reset Email at LEBS.com.vn";

        //                string link = "http://localhost:8748/Account/SentMailReset?EmailReset=" + EmailReset + "&code=" + Rcodew + "";
        //                string body = "Hi " + UserName + ",<br/>";
        //                body += "Someone asked to change your password on the website: Learnenglishbysong.com<br/>";
        //                body += "Click <a href='" + link + "'> this link </a>to reset your password<br/>";
        //                body += "If you have any questions, please contact us at '" + FromEmail + "'<br/>";
        //                body += "Thanks,<br/>";
        //                body += "'" + FromName + "'";

        //                CommonData.SendMail(FromEmail, FromName, EmailReset, subject, body);
                        

        //            }
        //            else
        //            {
        //                ModelState.AddModelError(string.Empty, "Email does not exits!");
        //                return View();
        //            }
        //            return Redirect(Url.Action("Index", "Home"));
        //        }
        //        else //Sent Phone
        //        {
        //            try
        //            {
        //                user = _userunitWork.User.Where(n => n.MobilePhone == model.PhoneNumber).First();
        //            }
        //            catch
        //            {

        //            }
        //            if (string.IsNullOrEmpty(user.MobilePhone))
        //            {
        //                ModelState.AddModelError(string.Empty, "Your phone does not exist!");
        //                return View(model);
        //            }

        //            if (string.IsNullOrEmpty(user.Sentsms) || user.Sentsms=="0")
        //            {
        //                ModelState.AddModelError(string.Empty, "Phone number is unavailable!");
        //                return View(model);
        //            }

        //            if (user.IsLocked == UserLocked.Locked)
        //            {
        //                ModelState.AddModelError(string.Empty, "This account already locked, please contact system administrator to unlocked ");
        //                return View(model);
        //            }

        //            if (user != null && user.Email != "")
        //            {
        //                string Rcodew = CommonData.CreatePassword(6);
        //                UserName = user.Username;
        //                user.Rcode = Rcodew;
        //                _userunitWork.User.Update(user);

        //                string body = "Hi " + UserName ;
        //                body += " Your secret code " + Rcodew;
        //                CommonData.SendSms(model.PhoneNumber, body);
        //                return Redirect(Url.Action("ConfirmSms", "Account", new { UserName = UserName}));
        //            }
        //            else
        //            {
        //                ModelState.AddModelError(string.Empty, "Email does not exits!");
        //                return View();
        //            }
                    
        //        }               
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}

        //[HttpGet]
        //public ActionResult ConfirmSms(string UserName)
        //{
        //    ValidateCodesms model = new ValidateCodesms();
        //    model.UserName = UserName;
        //    return View(model);
        //}

        //[HttpPost]
        //public ActionResult ConfirmSms(ValidateCodesms model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var username= model.UserName.Trim();
        //        var sms = model.SmsVerified.Trim();
        //        User user = new User();
        //        try
        //        {
        //            user = _userunitWork.User.All.ToList().Where(p => p.Username == username).First();
        //        }
        //        catch
        //        {
        //            var x = (from p in _userunitWork.User.All where p.Username == username select p);
        //            if (x.Any())
        //            {
        //                user = x.Single();
        //            }
                        
        //        }
              
        //        if (user == null || string.IsNullOrEmpty(user.Username) || user.Rcode == null || user.Rcode != model.SmsVerified)
        //        {
        //            ModelState.AddModelError("SmsVerified", "Your code is invalid!");
        //            return View(model);
        //        }
        //        else
        //        {
        //            string matkhau = CommonData.CreatePassword(6);
        //            string matkhaumahoa = CommonData.MD5(matkhau);

        //            string UserName = user.Username.ToString();
        //            user.Pass = matkhaumahoa;
        //            user.Rcode = "";
        //            _userunitWork.User.Update(user);

        //            string body = "Hi " + UserName;
        //            body += " Username " + UserName + " Password " + matkhau;
        //            try
        //            {
        //                CommonData.SendSms(user.MobilePhone, body);
        //                return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 0 }));
        //            }
        //            catch
        //            {
                        
        //                return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 1 }));
        //            }
                    
        //        }
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}

        //[HttpGet]
        //public ActionResult SentMailReset()
        //{
        //    string EmailReset = Request.QueryString["EmailReset"];
        //    string Rcode = Request.QueryString["code"];      
        //    string UserName = "";

        //    User user = _userunitWork.User.Where(n => n.Email == EmailReset).First();
        //    if (user.Rcode != Rcode || string.IsNullOrEmpty(user.Email))
        //    {
        //        return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 1 }));// unsuccess
        //    }        

        //    if (user!=null)
        //    {
        //        string matkhau = CommonData.CreatePassword(6);
        //        string matkhaumahoa = CommonData.MD5(matkhau);

        //        UserName = user.Username.ToString();
        //        user.Pass = matkhaumahoa;
        //        user.Rcode = "";
        //        _userunitWork.User.Update(user);

        //        string FromEmail = Settings.Default.EmailAdmin;
        //        string FromName = Settings.Default.DisplayName;
        //        string subject = "Reset Email at Learnenglishbysong.com.vn";

        //        string link = "http://localhost:8748/Account/LogOn";
        //        string body = "Hi " + UserName + ",<br/>";
        //        body += "You can login with the following credentials: <br/> Username: '" + UserName + "'<br/> Password: '" + matkhau + "' <br/>";
        //        body += "You can login <a href='" + link + "'> here </a><br/>";
        //        body += "If you have any questions, please contact us at '" + FromEmail + "'<br/>";
        //        body += "Thanks,<br/>";
        //        body += "'" + FromName + "'";

        //        CommonData.SendMail(FromEmail, FromName, EmailReset, subject, body);
        //        return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 0 }));
        //    }
        //    else
        //    {
        //        return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 1 }));
        //    }            
        //}

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        public ActionResult ResetSuccess(int Suc)
        {
            if (Suc == 0)
            {
                ViewBag.suc = "0";
                ViewBag.message = "Sent mail Successful!";
            }
            else if(Suc == 1)
            {
                ViewBag.suc = "1";
                ViewBag.message = "Sent mail Unsuccessful!";
            }
            else if (Suc == 2)
            {
                ViewBag.suc = "2";
                ViewBag.message = "Updated Successful!";
            }
            else
            {
                ViewBag.suc = "3";
                ViewBag.message = "Updated Unsuccessful!";
            }
            return View();
        }

        public ActionResult RegisterSuccess()
        {
            return View();
        }

        [Authorize]
        //[InitializeSimpleMembership]
        public ActionResult UserInformationShortPartial(string id)
        {
            EditUserModel UserModelview = new EditUserModel();
            HelloMinasan.Domain.User user = userService.Get(int.Parse(id));//userunitWork.User.Where(u => u.Username == id).FirstOrDefault();
            UserModelview.UserId = user.Id;
            UserModelview.Username = user.Username;
            UserModelview.Email = user.Email;
            UserModelview.DateOfBirth = (DateTime)user.DOB;
            UserModelview.Descriptions = "description";
            UserModelview.Avatar = "";// user.Avatar;
            UserModelview.LastAccess = DateTime.UtcNow;//(DateTime)user.LastLoginTime;// user.LastAccess.Value;
            UserModelview.Score = user.Point;
            UserModelview.Fullname = user.DisplayName;
            return PartialView(UserModelview);
        }

        public ActionResult UserInformation(string id)
        {
            
                EditUserModel UserModelview = new EditUserModel();
                HelloMinasan.Domain.User user = null;
                if (!String.IsNullOrEmpty(id))
                {
                    user = userService.Get(int.Parse(id)); //userunitWork.User.Where(u => u.Username == id).FirstOrDefault();
                }
                else if (!Request.IsAuthenticated)
                {
                    return Redirect(Url.Action("Index", "Home"));
                }
                if (user == null)
                {
                    user = userService.Get(int.Parse(User.Identity.Name)); //userunitWork.User.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
                }
                UserModelview.UserId = user.Id;
                UserModelview.Username = user.Username;
                UserModelview.Email = user.Email;
                UserModelview.DateOfBirth = (DateTime)user.DOB;
                UserModelview.Descriptions = "user.Descriptions";
                UserModelview.Avatar = "";// user.Avatar;
                UserModelview.UserStatus = user.Status.ToString();
                UserModelview.Score = user.Point;
                UserModelview.LastAccess = (DateTime)user.LastLoginTime;
                UserModelview.UserNotes = null;// user.UserNotes;
                UserModelview.Fullname = user.DisplayName;
                return PartialView(UserModelview);
        }

        public ActionResult StatisticPartial(string username, int score, int song, int test)
        {
            return PartialView(new WrapStatisticModels(username, score, song, test));

        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }         

        public ActionResult NotesPartial(int userId, int currentPage)
        {
            //bool owner = User.Identity.Name.ToLower() == username.ToLower() ? true : false;
            bool owner = CurrentUser.Id == userId ? true : false;
            NotesModel model = new NotesModel()
            {
                UserAccount = userId,
                isOwner = owner,
                PagingControl = new PagingHelper() { CurrentPageNumber = currentPage }
            };
            return PartialView(model);
        }

        //public ActionResult CreateNotes(string username, string noteContent)
        //{
        //    if (!Request.IsAuthenticated)
        //    {
        //        return Redirect(Url.Action("Logon", "Account"));
        //    }
        //    try
        //    {
        //        new UserIRepository().AddNotes(User.Identity.Name, noteContent);
        //    }
        //    catch
        //    {
        //        return Redirect(Url.Action("Logon", "Account"));
        //    }
        //    NotesModel model = new NotesModel()
        //    {
        //        UserAccount = username,
        //        isOwner = true,
        //        PagingControl = new PagingHelper()
        //    };
        //    return PartialView("NotesPartial", model);
        //}

        //public ActionResult DeleteNotes(string username, int currentPage, int id)
        //{
        //    try
        //    {
        //        if (!Request.IsAuthenticated)
        //        {
        //            throw new Exception(Resource.Song_RequiredAuthentication);
        //        }
        //        new UserIRepository().DeleteNotes(User.Identity.Name, id);
        //    }
        //    catch
        //    {
        //        return Redirect(Url.Action("Logon", "Account"));
        //    }
        //    NotesModel model = new NotesModel()
        //    {
        //        UserAccount = username,
        //        isOwner = true,
        //        PagingControl = new PagingHelper() { CurrentPageNumber = currentPage }
        //    };
        //    return PartialView("NotesPartial", model);
        //}

        //[Authorize]
        //public ActionResult UpdateProfile()
        //{
        //    string username = HttpContext.User.Identity.Name;
        //    User user = _userunitWork.User.All.Where(n => n.Username == username).First();
        //    UpdateProfileModel model = new UpdateProfileModel();
        //    model.UserId = user.UserId;
        //    model.Fullname = user.Fullname;
        //    model.Email = user.Email;
        //    model.DateOfBirth = user.DateOfBirth;
        //    model.MobilePhone = user.MobilePhone;
        //    model.Telephone = user.Telephone;
        //    model.Company = user.Company;
        //    model.Position = user.Possition;
        //    model.College = user.College;
        //    model.Avatar = user.Avatar;
        //    model.UserStatus = user.UserStatus;
        //    model.Descriptions = user.Descriptions;
        //    return View(model);

        //}

        //[HttpPost]
        //public ActionResult UpdateProfile(UpdateProfileModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        string username = HttpContext.User.Identity.Name;
        //        User user = _userunitWork.User.All.Where(n => n.Username == username).First();
        //        user.UserStatus = model.UserStatus;
        //        user.Fullname = model.Fullname;
        //        user.Email= model.Email;
        //        user.DateOfBirth=  model.DateOfBirth;
        //        user.Descriptions = model.Descriptions;
        //        user.Avatar = model.Avatar;
        //        user.MobilePhone= model.MobilePhone;
        //        user.Telephone=model.Telephone;
        //        user.Company= model.Company;
        //        user.Possition = model.Position;
        //        user.College = model.College;
        //        if (model.Checkvalidate == true)
        //        {
        //            try
        //            {
        //                string Rcodew = CommonData.CreatePassword(6);
        //                user.Rcode = Rcodew;
        //                _userunitWork.User.Update(user);

        //                CommonData.SendSms(model.MobilePhone, "Your code " + Rcodew);
        //                return Redirect(Url.Action("SmsVerifying", "Account", new { UserName = username }));
        //            }
        //            catch
        //            {
        //                ModelState.AddModelError(string.Empty, "Can't not sent sms to this number");
        //                return View(model);
        //            }
                    
        //        }
        //        else
        //        {
        //            try
        //            {
        //                _userunitWork.User.Update(user);
        //                return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 2 }));
        //            }
        //            catch 
        //            {
        //                ModelState.AddModelError(string.Empty, "Updated Unsuccessfull!");
        //                return View(model);
                        
        //            }                   
        //        }
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}

        //[HttpGet]
        //[Authorize]
        //public ActionResult SmsVerifying(string UserName)
        //{
        //    ValidateCodesms model = new ValidateCodesms();
        //    model.UserName = UserName;
        //    return View(model); 
        //}

        //[HttpPost]
        //[Authorize]
        //public ActionResult SmsVerifying(ValidateCodesms model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var username = model.UserName.Trim();
        //        var sms = model.SmsVerified.Trim();
        //        User user = new User();
        //        try
        //        {
        //            user = _userunitWork.User.All.ToList().Where(p => p.Username == username).First();
        //        }
        //        catch
        //        {
        //            var x = (from p in _userunitWork.User.All where p.Username == username select p);
        //            if (x.Any())
        //            {
        //                user = x.Single();
        //            }

        //        }

        //        if (user == null || string.IsNullOrEmpty(user.Username) || user.Rcode == null || user.Rcode != model.SmsVerified)
        //        {
        //            ModelState.AddModelError("SmsVerified", "Your code is invalid!");
        //            return View(model);
        //        }
        //        else
        //        {
        //            try
        //            {
        //                user.Rcode = "";
        //                user.Sentsms = "1";
        //                _userunitWork.User.Update(user);
        //                return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 2 }));
        //            }
        //            catch
        //            { 
        //                return Redirect(Url.Action("ResetSuccess", "Account", new { Suc = 3 }));
        //            }

        //        }
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}
    }
}
